<style>
    .form-copy {
        margin-right: 5px;
        border-radius: 0;
        box-shadow: none;
        padding: 4px 12px 7px 12px;
        font-size: 14px;
        line-height: 1.42857143;
        color: #555;
        border: 1px solid #ccc;
        transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
    }
    .select2 {
        margin-right: 5px
    }
</style>

<div class="btn-group">
    <form class="form-horizontal" action="{{ $action }}" pjax-container method="get">
        <input type="text" class="form-copy" style="width: 110px" id="start_date" name="start_date" value="{{ $filtersBlade['start_date'] }}" placeholder="开始日期">
        <input type="text" class="form-copy" style="width: 110px" id="end_date" name="end_date" value="{{ $filtersBlade['end_date'] }}" placeholder="截止日期">
        <select id="country_id" name="country_id">
            {{ $filtersBlade['country_id'] }}
        </select>
        <select id="classify_id" name="classify_id">
            {{ $filtersBlade['classify_id'] }}
        </select>
        <button class="btn btn-sm btn-info submit"><i class="fa fa-search"></i>&nbsp;&nbsp;{{ trans('admin.search') }}</button>
        <a style="margin-left: 10px;" href="{{ $action }}" class="btn btn-sm btn-default"><i class="fa fa-undo"></i>&nbsp;&nbsp;{{ trans('admin.reset') }}</a>
    </form>
    <div>
        <button class="btn btn-sm btn-twitter create-file"><i class="fa fa-retweet"></i>&nbsp;&nbsp;<span id="loading">生成核验报表</span></button>
    </div>
</div>
<script language="JavaScript" type="text/javascript">
    $(function () {
        $('#start_date').datetimepicker({"format":"YYYY-MM-DD","locale":"zh-CN","maxDate":moment(),"allowInputToggle":true});
        $('#end_date').datetimepicker({"format":"YYYY-MM-DD","locale":"zh-CN","maxDate":moment(),"allowInputToggle":true});
        $('#country_id').select2({placeholder: '国家', allowClear: true, width: '80px'});
        $('#classify_id').select2({placeholder: '品类', allowClear: true, width: '180px'});

        var checkForm = function(){
            var startDate = $('#start_date').val();
            var endDate = $('#end_date').val();
            if(!startDate){
                swal('开始日期为空', '', 'error');
                return false;
            }
            if(!endDate){
                swal('截止日期为空', '', 'error');
                return false;
            }
            var startTime = Date.parse(startDate);
            var endTime = Date.parse(endDate);
            if(startTime > endTime){
                swal('开始日期不能大于截止日期', '', 'error');
                return false;
            }
            var time = endTime - startTime;
            var day = time / 1000 / 60 / 60 / 24;
            if(day > 90){
                swal('日期间隔过大(MAX=90)', '', 'error');
                return false;
            }
            var countryId = $('#country_id').val();
            if(!countryId){
                swal('国家为空', '', 'error');
                return false;
            }
            var classifyId = $('#classify_id').val();
            if(!classifyId){
                swal('品类为空', '', 'error');
                return false;
            }
            return true;
        };

        $('.create-file').on('click', function(e) {
            e.preventDefault();
            if(checkForm()){
                var startDate = $('#start_date').val();
                var endDate = $('#end_date').val();
                var countryId = $('#country_id').val();
                var classifyId = $('#classify_id').val();
                $.ajax({
                    method: 'post',
                    url: '/xqc/operation/tc-ajax',
                    data: {
                        _token:"{{ csrf_token() }}",
                        action:'createFile',
                        param:{'startDate':startDate,'endDate':endDate,'countryId':countryId,'classifyId':classifyId}
                    },
                    beforeSend: function(){
                        $('#loading').text('报表生成中，请耐心等待。');
                        $('.create-file').attr('disabled',true);
                    },
                    success: function (data) {
                        $('#loading').text('生成核验报表');
                        $('.create-file').removeAttr('disabled');
                        $.pjax.reload('#pjax-container');
                        if (typeof data === 'object') {
                            if (data.status) {
                                toastr.success(data.message);
                            } else {
                                swal(data.message, '', 'error');
                            }
                        }
                    }
                });
            }
        });
    });
</script>
